<%@ page import="java.net.URLDecoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html class="loginHtml">
<head>
    <meta charset="utf-8">
    <title>在线考试系统</title>
    <base href="${pageContext.request.contextPath}/">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="icon" href="images/favicon.ico">
    <link rel="stylesheet" href="static/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="static/css/login.css" media="all"/>
    <link rel="stylesheet" href="static/css/bootstrap.css" media="all"/>
    <link rel="stylesheet" href="static/css/bootstrapValidator.min.css" media="all"/>
    <script type="text/javascript" src="static/layui/layui.js"></script>
    <script type="text/javascript" src="static/js/jquery.min.js"></script>
    <script type="text/javascript" src="static/js/bootstrap.js"></script>
    <script type="text/javascript" src="static/js/bootstrapValidator.min.js"></script>
    <script type="text/javascript" src="static/js/login.js"></script>
    <%
        String username;
        String password;

        Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            for (Cookie cookie : cookies) {

                if (cookie.getName().equals("username")) {
                    if (cookie.getValue() != null) {
                        /* 获取名为 username 的 Cookie 中的值并用 URL 解码 */
                        username = URLDecoder.decode(cookie.getValue(), "utf-8");
                        request.setAttribute("username", username);
                    }
                }
                if (cookie.getName().equals("password")) {
                    if (cookie.getValue() != null) {
                        /* 获取名为 username 的 Cookie 中的值并用 URL 解码 */
                        password = URLDecoder.decode(cookie.getValue(), "utf-8");
                        request.setAttribute("password", password);
                    }
                }
            }
        }
    %>
    <style>
        .layui-input.layui-unselect {
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="bg-body"></div>
<div class="login" onclick="clean()">
    <h1>在线考试系统登录</h1>
    <form id="frmLogin" class="layui-form" method="post"
          enctype="multipart/form-data" action="login" style="width: 260px">
        <div class="layui-form-item form-group">
            <input class="layui-input form-control" name="name" placeholder="请输入用户名" lay-verify="required"
                   type="text" value="${empty user.name ? username : user.name}" autocomplete="off">
        </div>
        <div class="layui-form-item form-group">
            <input class="layui-input form-control" name="password" placeholder="请输入密码" lay-verify="required"
                   type="password" value="${empty user.password ? password : user.password}" autocomplete="off">
        </div>
        <div class="layui-form-item form_code form-group">
            <input class="layui-input form-control" name="code" placeholder="验证码" lay-verify="required" type="text"
                   autocomplete="off">
            <div class="code"><img src="/kaptcha.jpg" width="90" height="33" id="mycode" style="border-radius: 4px">
            </div>
        </div>
        <div class="layui-form-item form-group">
            <select class="form-control" name="identityId">
                <option value="10000" ${(empty user.identityId ? cookie.identityId.value : user.identityId) == 10000 ? "selected" : ""}>学员登录</option>
                <option value="10001" ${(empty user.identityId ? cookie.identityId.value : user.identityId) == 10001 ? "selected" : ""}>教员登录</option>
                <option value="10002" ${(empty user.identityId ? cookie.identityId.value : user.identityId) == 10002 ? "selected" : ""}>管理员登录</option>
            </select>
        </div>
        <div class="layui-form-item form-group">
            <input type="checkbox" name="rememberMe" value="true" lay-skin="primary" checked title="记住帐号?">
        </div>
        <div class="layui-form-item form-group">
            <input type="button" class="layui-btn fa-registered" value="去注册" onclick="register()"/>
            <button class="layui-btn fa-registered" style="margin-left: 102px; width: 74px" type="submit">登录</button>
        </div>
    </form>
</div>

<script>
    $(function () {
        layui.use("layer", function () {
            var layer = layui.layer;
            var msg = "${errorMsg}";
            if (msg.length > 0) {
                layer.msg(msg, {time: 2000});
            }
        });
        setTimeout(function () {
            loginMethod();//确定登录方式
        }, 100);

        $('#frmLogin').bootstrapValidator({
            feedbackIcons: {}
            //,live:"disabled"
            , trigger: "blur"
            , fields: {
                name: {
                    validators: {
                        notEmpty: {message: '用户名不得为空！'},
                        stringLength: {
                            min: 2,
                            max: 10,
                            message: '用户名必须大于2小于10'
                        }
                    }
                },
                password: {
                    validators: {
                        notEmpty: {message: '密码不得为空！'},
                        stringLength: {
                            min: 6,
                            max: 18,
                            message: '密码必须大于6小于18'
                        }
                    }
                },
                code: {
                    validators: {
                        notEmpty: {message: '请输入验证码！'},
                        remote: {
                            url: 'checkCode',
                            type: 'post',
                            message: '验证码有误！'
                        }
                    }
                }
            }
        })
    });

    function loginMethod() {
        var identitys = ["学员登录", "教员登录", "管理员登录"];
        var identityId = "${user.identityId}";
        if ("${cookie.identityId.value}".length > 0) {
            identityId = "${cookie.identityId.value}";
        }

        $(".layui-anim.layui-anim-upbit").find("dd").each(function () {
            if ($(this).hasClass("layui-this")) {
                $(this).removeClass("layui-this");
            }
            if ($(this).attr("lay-value") == identityId) {
                $(".layui-input.layui-unselect").val(identitys[parseInt(identityId) - 10000]);
                $(this).addClass("layui-this")
            }
        });
    }

    function register() {
        window.location.href = "/showRegister";
    }

    function clean() {
        $('#errorMsg').text('');
    }
</script>
</body>
</html>